<?php


function excel_khk_kalkulasi(){
	$header = array(
		'NIK'=>array(),  
		'Nama'=>array(),   
		'Jam Otomatis'=>array(),  
		'Tunjangan'=>array(), 
	);
	
	$query = " SELECT  a.karyawan_id ,a.nik, a.nhari ,a.nominal FROM temp_tmn_khk a
				INNER JOIN karyawan b ON a.karyawan_id = b.karyawan_id  
				";
	$result = my_query($query);
	while($ey = my_fetch_array($result )){ 
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		$row[] = array( 
			'nik'=>  "'".sprintf('%07d',$ey['nik']),    
			'nama'=>ucfirst($karyawan['nama']),
			'baru'=>  $ey['nhari'], 
			'lama'=>  $ey['nominal'] , 
		);
	}
	
	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas ,4 ,false ); 	
}

function proses_khk(){
	my_query("TRUNCATE TABLE `temp_tmn_khk` ");
	$end=get_batas_tmt() ;
	$query = " SELECT a.karyawan_id,a.nik FROM karyawan a
				INNER JOIN kekaryawanan b ON b.karyawan_id = a.karyawan_id 
				WHERE b.tmb  <= '{$end}' 
				AND a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
				AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE tmt <= '{$end}') 	
			";	 
	$result = my_query($query);
	$total_records = my_num_rows($result ); 
	if(!($total_records > 0)){
		return 0;
	}
	while($ey = my_fetch_array($result)){ 
		$harikerjalebih = total_khk($ey['karyawan_id']);
		$kekaryawanan = my_get_data_by_id( 'kekaryawanan' , 'karyawan_id' , $ey['karyawan_id']);
		$jhit = $harikerjalebih * 1.5 ;
		$nominal = ( $jhit / 173 ) * $kekaryawanan['gapok_km'];
		if($jhit == 0)continue;
	 
		$datas = array(
			'nik'	=> my_type_data_str($ey['nik']),
			'karyawan_id'	=> my_type_data_str($ey['karyawan_id']),
			'nhari'	=> my_type_data_str($jhit),
			'nominal'	=>my_type_data_str($nominal)
		);
		my_insert_record('temp_tmn_khk' , $datas); 
		 
	}
	return true;
}


  
function total_khk($karyawan_id  ){
	$query = "SELECT COUNT(*) AS ttl FROM temp_tmn_hari WHERE karyawan_id = {$karyawan_id} "; 
		$query .= " AND 
		(  
			status_absen = '1' OR 
   
status_absen = '5' OR
 
status_absen = '7' OR
 
status_absen = '9' OR
			status_absen = '3' 
		)"; 
	$result = my_query($query);
	$row = my_fetch_array($result);
	return $row['ttl'];
} 
 
function list_karyawan_khk($periode_id){
	my_set_code_js('
		function confirmRst(){
			var t = confirm(\'Yakin akan reset data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=rst\';
			}
			return false;
		}
		
		function cnProses(){
			var t = confirm(\'Proses data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=proses\';
			}
			return false;
		}
		
	');
	$header = array(
		'NIK'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'Nama'=>array('style'=>'text-align:left;border-bottom:2px solid;width:35%'),  
		'Jabatan'=>array('style'=>'text-align:left;border-bottom:2px solid;width:30%'),  
		'KHK'=>array('style'=>'text-align:center;border-bottom:2px solid;width: 5%'),  
		'Tunjangan'=>array('style'=>'text-align:right;border-bottom:2px solid;width:20%'), 
	);
	if(isset($_GET['key']))
	$query = "SELECT   a.karyawan_id , a.nhari ,a.nominal FROM temp_tmn_khk a
				INNER JOIN   karyawan c  ON a.karyawan_id = c.karyawan_id
		INNER JOIN biodata d ON d.biodata_id = c.biodata_id
		WHERE c.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable) 
		AND ( c.nik='{$_GET['key']}' OR d.nama LIKE '%{$_GET['key']}%' )
		 ORDER BY c.nik ASC
		";
	else
	$query = " SELECT  a.karyawan_id , a.nhari ,a.nominal FROM temp_tmn_khk a
				INNER JOIN karyawan b ON a.karyawan_id = b.karyawan_id  
				";
	$result = my_query($query);
	$total_records = my_num_rows($result );
	$scroll_page =7;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		  
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&periode_id={$_GET['periode_id']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	
	$kgPagerOBJ = new kgPager();
			$kgPagerOBJ->pager_set(
				$pager_url, 
				$total_records, 
				$scroll_page, 
				$per_page, 
				$current_page, 
				$inactive_page_tag, 
				$previous_page_text, 
				$next_page_text, 
				$first_page_text, 
				$last_page_text ,$pager_url_last); 
				
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while($ey = my_fetch_array($result)){
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		if(empty($karyawan['gelar'])){
			$nama_karyawan=ucfirst($karyawan['nama']) ;
		}else{
			$nama_karyawan=trim(ucfirst($karyawan['nama'])).", ".$karyawan['gelar'] ;
		}

		$jabatan = my_get_data_by_id('jabatan' ,'jabatan_id' , ($karyawan['jabatan_id']<>'' ? $karyawan['jabatan_id'] : 0 ));
		
		$editproperty = array(
						'href'=>'index.php?com=pr_upload_gaji&task=edit&komponen_gaji_id='.$komponen_gaji_id.'&id='.$ey['gaji_upload_view_id'],
						'rel'=>'facebox',
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		 
		$deleteproperty = array(
						'href'=>'javascript:; ',
						'onclick'=>'javascript:confirmDelete('.$ey['tmn_khk_id'].');',
						'title'=>'Delete'
				);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		$row[] = array(
			'nik'=>position_text_align ($karyawan['nik'] ,'center'),    
			'gd'=> $nama_karyawan ,      
			'jabatan'=> $jabatan['nama_jabatan']   ,
			'khk'=> position_text_align( $ey['nhari'] , 'center'),
			'tunj'=> position_text_align( rp_format($ey['nominal']) , 'right'),
		); 
	}
	$datas = table_rows($row);
	$paging = $kgPagerOBJ ->showPaging();	
//	$periode_gajis = my_get_data_by_id('pr_periode','periode_id',$periode_id); 	 
	$search = '<form method="GET"><input type="hidden" name="com" value="'.$_GET['com'].'" />
	<span style="font:10px verdana"><b>Pencarian cepat</b></span> 
	<input type="hidden" name="periode_id" value="'.$periode_id.'"/>
	<br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" />
	<input class="simple_search" type="submit" value=" CARI " /> </form>';

	$view ='<table width="90%" cellspacing="0" cellpadding="4"><tr>'; 
	$view .='<table width="100%"   style="border-collapse:collapse;border-color:white" cellspacing="0" cellpadding="2">';
	//$view .= form_field_display( '<span class="label_form"><b>'.date('m-Y' $periode_gajis['nama_komponen_gaji']).'</b></span>', "<b>Komponen Gaji</b>"    ); 
	
	//$button_import =   ($komponen_gajis['reset_tutup_bulan'] =='yes') ? '';
	$view .= '<tr><td><br/>'.$search .'</td><td valign="bottom"><div style="width:100%;text-align:right;">
	'.$button_import .' 
	<input type="button" value="Excel" onclick="javascript:location.href=\'index.php?com='.$_GET['com'].'&task=excel_report\'" class="main_button" /> 
	<input type="button" value="Reset" onclick="javascript:confirmRst()" class="main_button" /> 
	<input type="button" value="Proses" onclick="javascript:cnProses();" class="main_button" /> 
	 </div></td></tr>'   ; 
			return  $view.   table_builder($header , $datas ,  6 , false ,$paging );	

}

function upload_khk_form_page(){
	$view = form_header( "upload" , "upload"  );
	
	$file = array(
			'name'=>'file',
			'value'=>(isset($_POST['file'])? $_POST['file'] : $fields['file']),
			'id'=>'file' ,'size'=>'70',
			'type'=>'file'
	);
	$form_nominal = form_dynamic($file);
	$view .= form_field_display( $form_nominal , "File CSV khk" );
	
	$submit = array(
		'value' => ' Proses ',
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
} 

function stor_temp_khk_table($kid,$nik,$gapok,$value,$dest){
	
	$tunjangan = KONSTANTA_TUNJANGAN_khk * $value * $gapok/ 173;
	$datas = array(
		'karyawan_id'=>my_type_data_str($kid),
		'nik'=>my_type_data_str(sprintf('%07d',$nik)),
		'nhari'=>my_type_data_int($value), 
		'nominal'=>my_type_data_int($tunjangan), 
	);
	my_insert_record('temp_tmn_khk',$datas);
}

function upload_form_page_submit(){
	$dest ='files/csv/file_khk_'.date('Ymd_his').'.csv';
	$ori = $_FILES['file']['tmp_name'];
	$upload = move_uploaded_file($ori , $dest); 
	if($upload){
		my_query("TRUNCATE TABLE temp_tmn_khk");
		$handle = fopen($dest, "r"); 
		while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
			if(trim($data[0])<>'' && $data[1]<> ''){
				$karyawan_id =check_karyawan_by_nik(sprintf('%07d',$data[0]));
				$karyawan = loaddata_karyawan($karyawan_id);
				stor_temp_khk_table($karyawan_id,$data[0],$karyawan['gapok_km'],$data[1],$dest);
			}
		}
		fclose($handle);
	}
	return $dest ;
}


function upload_form_validate(){
	$errsubmit = false;
	$err = array();
	
	$exts = array('csv','xls');
	$ext = end(explode("." , strtolower( $_FILES['file']['name'] ) ) );
	if( ! is_file($_FILES['file']['tmp_name']) ){
		$errsubmit = true;
		$err[] = "Tidak ada file yang disertakan";
	}
	elseif( ! in_array($ext , $exts)  ){
		$errsubmit = true;
		$err[] = "File tidak dikenali";
	}	
	
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function upload_form_page(){
	
}